package com.sxygsj.baselibrary.network

import com.google.gson.Gson
import okhttp3.RequestBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

/**
 * @Author LD
 * @Time 2020/9/2 14:18
 * @Describe 自定义解析类
 * @Modify
 */
internal class ResponseConverterFactory private constructor(gson: Gson?) :
    Converter.Factory() {
    private val gson: Gson
    override fun requestBodyConverter(
        type: Type,
        parameterAnnotations: Array<Annotation>,
        methodAnnotations: Array<Annotation>,
        retrofit: Retrofit
    ): Converter<*, RequestBody> {
        //返回我们自定义的Gson响应体变换器
        return GsonResponseBodyConverter(
            gson,
            type
        )
    }

    override fun responseBodyConverter(
        type: Type,
        annotations: Array<Annotation>,
        retrofit: Retrofit
    ): GsonResponseBodyConverter<Any?> {
        return GsonResponseBodyConverter<Any?>(
            gson,
            type
        )
    }

    companion object {
        @JvmOverloads
        fun create(gson: Gson? = Gson()): ResponseConverterFactory {
            return ResponseConverterFactory(gson)
        }
    }

    init {
        if (gson == null) {
            throw NullPointerException("gson == null")
        }
        this.gson = gson
    }
}